package com.coderdream.chapter16.mediator.sample;

import java.awt.CheckboxGroup;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginFrame extends Frame implements ActionListener, Mediator {

	private ColleagueCheckbox checkGuest;
	private ColleagueCheckbox checkLogin;
	private ColleagueTextField textUser;
	private ColleagueTextField textPass;
	private ColleagueButton buttonOk;
	private ColleagueButton buttonCancel;

	/**
	 * 構造函數：產生并配置好Colleague之后再輸出到畫面上
	 * 
	 * @param title
	 */
	public LoginFrame(String title) {
		super(title);
		setBackground(Color.lightGray);

		// 使用頁面編排，產生4×2的畫面
		// 使用页面编排，产生4×2的画面
		setLayout(new GridLayout(4, 2));

		// 产生Colleague
		createColleagues();

		// 配置
		add(checkGuest);
		add(checkLogin);
		add(new Label("Username:"));
		add(textUser);
		add(new Label("Password:"));
		add(textPass);
		add(buttonOk);
		add(buttonCancel);

		// 有效、无效的初始设定
		colleagueChanged(checkGuest);

		// 输出到画面上
		pack();
		setVisible(true);// 取代过时的show()方法
	}

	/**
	 * 产生Colleague
	 * 
	 * (non-Javadoc)
	 * 
	 * @see com.coderdream.chapter16.mediator.Mediator#createColleagues()
	 */
	public void createColleagues() {
		// 产生
		CheckboxGroup g = new CheckboxGroup();
		checkGuest = new ColleagueCheckbox("Guest", g, true);
		checkLogin = new ColleagueCheckbox("Login", g, true);
		textUser = new ColleagueTextField("", 10);
		textPass = new ColleagueTextField("", 10);
		textPass.setEchoChar('*');// 设置此文本字段的回送字符。
		// textPass.setEchoChar(0);//设置此文本字段的回送字符。
		buttonOk = new ColleagueButton("OK");
		buttonCancel = new ColleagueButton("Cancel");

		// Mediator部分
		checkGuest.setMediator(this);
		checkLogin.setMediator(this);
		textUser.setMediator(this);
		textPass.setMediator(this);
		buttonOk.setMediator(this);
		buttonCancel.setMediator(this);

		// Listener部分
		checkGuest.addItemListener(checkGuest);
		checkLogin.addItemListener(checkLogin);
		textUser.addTextListener(textUser);
		textPass.addTextListener(textPass);
		buttonOk.addActionListener(this);
		buttonCancel.addActionListener(this);
	}

	/**
	 * 根据Colloeague的消息，判断各Colleague的有效/无效
	 */
	public void colleagueChanged(Colleague c) {
		if (c == checkGuest || c == checkLogin) {
			if (checkGuest.getState()) {// Guest模式
				textUser.setColleagueEnabled(false);
				textPass.setColleagueEnabled(false);
				buttonOk.setColleagueEnabled(true);
			} else {// Login模式
				textUser.setColleagueEnabled(true);
				userpassChanged();
			}
		} else if (c == textUser || c == textPass) {
			userpassChanged();
		} else {
			System.out.println("colleagueChanged:unknow colleague = " + c);
		}
	}

	/**
	 * textUeer或textPass有变动
	 */
	private void userpassChanged() {
		if (textUser.getText().length() > 0) {
			textPass.setColleagueEnabled(true);
			if (textPass.getText().length() > 0) {
				buttonOk.setColleagueEnabled(true);
			} else {
				buttonOk.setColleagueEnabled(false);
			}
		} else {
			textPass.setColleagueEnabled(false);
			buttonOk.setColleagueEnabled(false);
		}
	}

	public void actionPerformed(ActionEvent e) {
		System.out.println("" + e);
		System.exit(0);
	}
}
